package at.ac.univie.zsu.aguataplan.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import at.ac.univie.zsu.aguataplan.dao.ClubDao;
import at.ac.univie.zsu.aguataplan.dao.Dao;
import at.ac.univie.zsu.aguataplan.dao.TeamDao;
import at.ac.univie.zsu.aguataplan.domain.Club;
import at.ac.univie.zsu.aguataplan.domain.Team;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;

/**
 * @author gerry
 * 
 */
@Service
public class TeamManagerBean implements TeamManager {
	private static Logger log = Logger.getLogger(TeamManagerBean.class);

	@Autowired
	private TeamDao teamDao;

	@Autowired
	private ClubDao clubDao;

	@Autowired
	private Dao dao;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public TeamManagerBean() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	@Override
	public void cleanTeam() throws DatabaseException {
		try {
			teamDao.cleanTeam();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entries", e.getMessage());
		}
	}

	// --------------

	@Override
	public Team addTeam(String name, Club club) throws DatabaseException {
		Team team = new Team();
		team.setName(name);

		Club c;

		try {
			c = clubDao.getClubById(club.getId());
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not load entry", e.getMessage());
		}

		c.addTeam(team);
		team.setRefClub(club);

		Team retTeam;
		try {
			retTeam = teamDao.addTeam(team);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not insert entry", e.getMessage());
		}

		try {
			clubDao.mergeClub(c);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}

		return retTeam;
	}

	@Override
	public Team getTeamById(long id) throws DatabaseException {
		Team retTeam = null;
		try {
			retTeam = dao.find(Team.class, id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entry", e.getMessage());
		}
		return retTeam;
	}

	@Override
	public void deleteTeam(long id) throws DatabaseException {
		Team team;
		try {
			team = teamDao.getTeam(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not load entry", e.getMessage());
		}

		Club club = team.getRefClub();
		club.deleteTeam(id);

		try {
			clubDao.mergeClub(club);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}

		try {
			teamDao.deleteTeam(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}
	}

	@Override
	public List<Team> getTeamList() throws DatabaseException {
		List<Team> list = null;

		try {
			list = teamDao.getTeamList();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return list;
	}

	@Override
	public Team mergeTeam(Team team) throws DatabaseException {
		Team retTeam = null;
		try {
			retTeam = teamDao.mergeTeam(team);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}
		return retTeam;
	}

	@Override
	public Integer getTeamListPosition(long id) throws DatabaseException {
		List<Team> list = null;
		Integer pos = -1;

		try {
			list = teamDao.getTeamList();
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getId() == id) {
					pos = i;
					break;
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}

		return pos;
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public TeamDao getTeamDao() {
		return teamDao;
	}

	public void setTeamDao(TeamDao teamDao) {
		this.teamDao = teamDao;
	}

	public ClubDao getClubDao() {
		return clubDao;
	}

	public void setClubDao(ClubDao clubDao) {
		this.clubDao = clubDao;
	}

	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

}
